package com.anolesoft.qxgl.service.employee.impl;

import com.anolesoft.common.core.domain.LoginUser;
import com.anolesoft.common.security.util.SecurityUtils;
import com.anolesoft.qxgl.dto.employee.EmployeeDto;
import com.anolesoft.qxgl.service.employee.inf.EmployeeServiceInf;
import com.anolesoft.qxgl.service.employee.mapper.EmployeeServiceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.stereotype.Service;

@Service
public class EmployeeServiceInfImpl implements EmployeeServiceInf {
    @Autowired
    private EmployeeServiceMapper employeeServiceMapper;

    /**
     * 修改密码
     *
     * @param employeeDto
     * @return
     */
    @Override
    public int updatePassword(EmployeeDto employeeDto) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        employeeDto.setFactoryId(loginUser.getFacCode());
        String hashpw = BCrypt.hashpw(employeeDto.getPassword(), BCrypt.gensalt());
        employeeDto.setHashpw(hashpw);
        int updatePassword = employeeServiceMapper.updatePassword(employeeDto);
        return updatePassword;
    }

    /**
     * 重置密码
     *
     * @param employeeDto
     * @return
     */
    @Override
    public int resetPassword(EmployeeDto employeeDto) {
        // 默认重置后密码为111111
        String password = "111111";
        employeeDto.setPassword(password);
        String hashpw = BCrypt.hashpw(password, BCrypt.gensalt());
        employeeDto.setHashpw(hashpw);
        int resetPassword = employeeServiceMapper.resetPassword(employeeDto);
        return resetPassword;
    }
}
